﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AssessmentTool.PresentationLogic;
using AssessmentTool.PresentationLogic.EventArguments;

namespace AssessmentTool.Controls
{
    public partial class HelpModal : ChildWindow
    {
        bool modal = true;
        public HelpModal()
        {
            InitializeComponent();
         
            EventDispatcher.getInstance().subscrive("CLOSEMODAL", onCloseModal);
            EventDispatcher.getInstance().subscrive("SHOWHELP", showModal);
        }
        
        public void onCloseModal(EventArgs e)
        {
            if (modal)
                this.Visibility = Visibility.Collapsed;
        }

        public void showModal(EventArgs e)
        {
            if ((this.Visibility == Visibility.Visible) && !modal)
            {
                return;
            }
            
            
            ShowHelpEventArgs ev = (ShowHelpEventArgs)e;
            modal = ev.ShowModal;
            
            //WindowWidth = ev.Width + "";
            //WindowHeight = ev.Height + "";

            TextBlock tb = null;
            ScrollViewer view = null;
            if (!String.IsNullOrEmpty(ev.Text))
            {
                view = new ScrollViewer();
                view.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                view.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;



                tb = new TextBlock();
                tb.Style = (Style)App.Current.Resources["NormalText"];
                tb.TextWrapping = TextWrapping.Wrap;

                String[] strings = ev.Text.Split('$');
                if (!modal)
                {
                    tb.Inlines.Add(new Run() { Text = ev.Titolo });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new LineBreak());
                }
                foreach (String s in strings)
                {
                    tb.Inlines.Add(new Run() { Text = "• " + s });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new LineBreak());
                }
                tb.VerticalAlignment = VerticalAlignment.Stretch;
                view.Content = tb;
                BoxContent.Content = view;
            }
            if (ev.ShowModal)
            {
                tb.FontSize = 10;
                LayoutRoot.Width = ev.Width;
                //LayoutRoot.Height = ev.Hei
               // WindowHeight = tb.Height + "";
                this.HorizontalAlignment = HorizontalAlignment.Left;
                this.VerticalAlignment = VerticalAlignment.Top;
                this.Margin = ev.Margin;
                HasCloseButton = false;
                LayoutRoot.MaxHeight = ev.Height;
                view.MaxHeight = ev.Height;
                modal = true;

            }
            else
            {
                tb.FontSize = 12;
                modal = false;
               // WindowHeight = tb.Height + "";
                this.HorizontalAlignment = HorizontalAlignment.Center;
                this.VerticalAlignment = VerticalAlignment.Center;
                this.Margin = new Thickness(0, 0, 0, 0);
                LayoutRoot.Width = double.NaN;
                LayoutRoot.MaxHeight = ev.Height;
                HasCloseButton = true;
                view.MaxHeight = ev.Height;
            }
            if (modal)
            {
                this.Visibility = Visibility.Visible;

            }
            else
            {
                base.Show();
            }
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

